package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.*;
import com.goods.business.service.OutStockServices;
import com.goods.common.model.business.*;
import com.goods.common.vo.business.*;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author ：MJS
 * @date ：Created in 2021/6/10  10:00
 * @description：TODO
 */

@Service
public class OutStockServicesImpl implements OutStockServices {
    @Autowired(required = false)
    private OutStockMapper outStockMapper;
    @Autowired(required = false)
    private OutStockInfoMapper outStockInfoMapper;
    @Autowired(required = false)
    private ConsumerMapper consumerMapper;
    @Autowired(required = false)
    private ProductMapper productMapper;
    @Autowired(required = false)
    private ProductStockMapper productStockMapper;
    //加载出库表格数据
    @Override
    public PageVO<OutStockVO> findOutStockList(Integer pageNum, Integer pageSize, OutStockVO outStockVO) {
        //已经拥有的出货的表
        PageHelper.startPage(pageNum, pageSize);
        Example o = new Example(OutStock.class);
        //出库单号
        String outNum = outStockVO.getOutNum();
        //发放类型
        Integer type = outStockVO.getType();
        //发放状态
        Integer status = outStockVO.getStatus();
        Example.Criteria criteria = o.createCriteria();

        if (outNum != null && !"".equals(outNum)) {
            criteria.andEqualTo("outNum", outNum);
        }
        if (type != null) {
            criteria.andEqualTo("type", type);
        }
        if (status != null) {
            criteria.andEqualTo("status", status);
        }
        List<OutStock> outStockList = outStockMapper.selectByExample(o);

        List<OutStockVO> outStockVos = outStockList.stream().map(outStock -> {
            OutStockVO outStockVO1 = new OutStockVO();
            BeanUtils.copyProperties(outStock, outStockVO1);
            Long consumerId = outStockVO1.getConsumerId();
            //建立物资去处表
            Consumer consumer = new Consumer();
            consumer.setId(consumerId);
            consumer = consumerMapper.selectByPrimaryKey(consumer);
            String name = consumer.getName();
            String phone = consumer.getPhone();
            outStockVO1.setName(name);
            outStockVO1.setPhone(phone);

            return outStockVO1;
        }).collect(Collectors.toList());

        PageInfo<OutStock> info = new PageInfo(outStockList);
        return new PageVO<>(info.getTotal(), outStockVos);
    }

    //查询发放明细
    @Override
    public OutStockDetailVO detail(Long id, Integer pageNum) {
        //创建最终的返回对象
        OutStockDetailVO outStockDetailVO = new OutStockDetailVO();
        //1.通过id查询到outStock对象
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        //2.拿到对象查询的关联表和consumer表
        Example o = new Example(Consumer.class);
        o.createCriteria().andEqualTo("id", outStock.getConsumerId());
        //consumer表对象
        Consumer consumer = consumerMapper.selectOneByExample(o);
        //转换成consumerVo封装到明细表
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer, consumerVO);
        outStockDetailVO.setConsumerVO(consumerVO);
        //3.去到出库详情表  就是一个关联表 关联 product
        Example o2 = new Example(OutStockInfo.class);
        o2.createCriteria().andEqualTo("outNum", outStock.getOutNum());
        //中间表对象集合
        List<OutStockInfo> outStockInfos = outStockInfoMapper.selectByExample(o2);

        //4.通过关联表数据拿到product表数据
        List<OutStockItemVO> outStockItemList = outStockInfos.stream().map(outStockInfo -> {
            Example o3 = new Example(Product.class);
            o3.createCriteria().andEqualTo("pNum", outStockInfo.getPNum());
            Product product = productMapper.selectOneByExample(o3);

            OutStockItemVO outStockItemVO = new OutStockItemVO();

            BeanUtils.copyProperties(product, outStockItemVO);
            outStockItemVO.setCount(outStockInfo.getProductNumber());
            return outStockItemVO;
        }).collect(Collectors.toList());
        //数据整合封装返回
        outStockDetailVO.setItemVOS(outStockItemList);
        outStockDetailVO.setOperator(outStock.getOperator());
        outStockDetailVO.setOutNum(outStock.getOutNum());
        outStockDetailVO.setType(outStock.getType());
        outStockDetailVO.setStatus(outStock.getStatus());
        outStockDetailVO.setTotal(outStockItemList.size());

        return outStockDetailVO;
    }
    //移入回收站
    @Override
    public void remove(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        outStock.setStatus(OutStock.DELETE);
        Example o = new Example(outStock.getClass());
        o.createCriteria().andEqualTo("id",id);
        outStockMapper.updateByExample(outStock,o);
    }
    //从回收站恢复
    @Override
    public void back(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        outStock.setStatus(OutStock.NORMAL);
        Example o = new Example(outStock.getClass());
        o.createCriteria().andEqualTo("id",id);
        outStockMapper.updateByExample(outStock,o);
    }
    //从回收站永久删除
    @Override
    public void delete(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        outStockMapper.delete(outStock);
    }
    //添加发放单
    @Override
    public void addOutStock(OutStockVO outStockVO) {
        Long consumerId = outStockVO.getConsumerId();
        if(consumerId==null){
            //1.新建consumer表  将outStockVO表中的数据  放入consumer
            Consumer consumer = new Consumer();
            BeanUtils.copyProperties(outStockVO,consumer);
            consumer.setCreateTime(new Date());
            consumer.setModifiedTime(new Date());
            consumerMapper.insert(consumer);
            //获取刚刚添加信息的主键Id
            consumerId= consumer.getId();
        }
        //设置uuid
        String outNum = UUID.randomUUID().toString();
        //获取products
        List<Object> products = outStockVO.getProducts();
        for (Object product : products) {
            //新建outStockInfo表
            OutStockInfo outStockInfo = new OutStockInfo();
            Map product1 = (Map) product;
            Object productId = product1.get("productId");
            Integer productNumber = (Integer)product1.get("productNumber");
            Example o = new Example(Product.class);
            o.createCriteria().andEqualTo("id",productId);
            Product product2 = productMapper.selectOneByExample(o);
            //获取pNum
            String pNum = product2.getPNum();
            outStockInfo.setPNum(pNum);
            outStockInfo.setCreateTime(new Date());
            outStockInfo.setModifiedTime(new Date());
            outStockInfo.setOutNum(outNum);
            outStockInfo.setProductNumber(productNumber);
            outStockInfoMapper.insert(outStockInfo);
        }
        //获取出库单种类数量
        int productNum = products.size();
        //建实体类
        OutStock outStock = new OutStock();
        //vo传实体类
        BeanUtils.copyProperties(outStockVO,outStock);
        outStock.setCreateTime(new Date());
        outStock.setConsumerId(consumerId);
        //设置商品总类数量
        outStock.setProductNumber(productNum);
        //设置uuid
        outStock.setOutNum(outNum);
        outStock.setOperator("admin");
        outStock.setStatus(OutStock.AUDIT);
        outStockMapper.insert(outStock);
    }

    //出库单审核
    @Transactional
    @Override
    public void publish(Long id) {
        //查询outStock
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        //改变outStock的状态
        outStock.setStatus(OutStock.NORMAL);
        //获取出库单编号
        String outNum = outStock.getOutNum();
        //建outStockInfo表
        OutStockInfo outStockInfo = new OutStockInfo();
        outStockInfo.setOutNum(outNum);
        //查询outStockInfo表
        List<OutStockInfo> outStockList = outStockInfoMapper.select(outStockInfo);
        //遍历
        for (OutStockInfo stockInfo : outStockList) {
            //获取编号
            String pNum = stockInfo.getPNum();
            //获取数量
            Integer productNumber = stockInfo.getProductNumber();

            Example o = new Example(ProductStock.class);
            o.createCriteria().andEqualTo("pNum",pNum);
            ProductStock productStock = productStockMapper.selectOneByExample(o);
            productStock.setStock(productStock.getStock()-productNumber);
            productStockMapper.updateByExample(productStock,o);
        }
        Example o = new Example(outStock.getClass());
        o.createCriteria().andEqualTo("id",id);
        outStockMapper.updateByExample(outStock,o);

    }
}
